package academic.ui.client.view;


import java.util.HashMap;
import java.util.Map;

import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.ViewImpl;

import ui.client.GenericClickable;
import ui.client.GenericContainerTag;

import academic.ui.client.config.CSSConstants;
import academic.ui.client.presenter.AccountPresenter;
import academic.ui.client.widgets.SliderPanel;


public class AccountView extends ViewImpl implements AccountPresenter.MyView {

	public GenericContainerTag myAccountDiv = new GenericContainerTag("div");
	
	private GenericContainerTag tabContainer = new GenericContainerTag("div");
		
	private GenericClickable myDeals = new GenericClickable("a","My Deals");
	public SliderPanel tabContent = new SliderPanel();

	private GenericClickable myGroups = new GenericClickable("a","My Groups");

	private GenericClickable myAccount = new GenericClickable("a","My Account");
		
	
	private Map<String,GenericClickable> tabList = new HashMap<String,GenericClickable>();
	
	
	@Inject
	public AccountView(CSSConstants css) {
		myAccountDiv.setStyleName("myAccount");
		GenericContainerTag ul = new GenericContainerTag("ul");
		ul.setStyleName("tabs");
		
		GenericClickable li1 = new GenericClickable("li");
		GenericClickable li2 = new GenericClickable("li");
		GenericClickable li3 = new GenericClickable("li");
		GenericClickable li4 = new GenericClickable("li");
		
		tabList.clear();
		tabList.put(AccountPresenter.MYDEALSTAB,li1);
		tabList.put(AccountPresenter.MYGROUPSTAB,li2);
		tabList.put(AccountPresenter.MYACCOUNTTAB, li3);
	
		li1.add(myDeals);
		li2.add(myGroups);
		li3.add(myAccount);
		ul.add(li1);ul.add(li2);ul.add(li3);ul.add(li4);

		myAccountDiv.add(ul);
		
		myAccountDiv.add(tabContainer);
		tabContainer.setStyleName("tab_container");
		tabContainer.add(tabContent);
	
	}
	
	
	@Override
	public Widget asWidget() {
		return myAccountDiv;
	}
	
	
	@Override
	public void addContent(Object slot, Widget w) {
		if(slot==AccountPresenter.tabSlot)
		{
			tabContent.clear();
			tabContent.add(w);
		}
	}


	@Override
	public void updateTab(String param) {
		resetTabs();
		for(String key : tabList.keySet())
			if(key.equals(param))
			{
				tabList.get(key).setStyleName("active");
				DeferredCommand.addCommand(new Command() {

					@Override
					public void execute() {
						tabContent.setOpen(true);

					}
					
				});
				
			}
	}


	private void resetTabs() {
		for(String key : tabList.keySet())
		{
			tabList.get(key).removeStyleName("active");
			tabContent.setOpen(false);
		}
		
	}
	
	@Override
	public Map<String,GenericClickable> getTabHandlers() {
		return tabList;
	}

}
